<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020-02-20
 * Time: 9:08
 */

namespace app\admin\controller;

use app\admin\controller\Base;
use app\admin\model\Menu;
use think\Config;

class Atype extends Base
{
    public function index(){
        $list = Menu::order('sort','desc')->paginate(Config::get('paginate')['list_rows']);
        $this->assign('list',$list);
        return $this->fetch();
    }

    public function add(){
        $data = array(
            'title' => $this->request->param('title'),
            'status' => 1,
            'sort' => 0,
            'is_show' =>1,
            'icon_code' => '&#xe83c;'
        );
        if(count($data)>=4){
            Menu::create($data);
            echo '<script>alert("操作完成");window.location.href="index.html"</script>';
        }else{
            echo '<script>alert("请正确输入相关信息");</script>';
        }
    }

    public function edit(){
        $menu = Menu::get($this->request->param('id'));
        $this->assign('info',$menu);
        return $this->fetch();
    }

    public function editdo(){
        $id = $this->request->param('id');
        $data = array(
            'id' => (int)$id,
            'title' => $this->request->param('title'),
            'is_show' => $this->request->param('is_show')?$this->request->param('is_show'):0,
            'icon_code' => $this->request->param('icon_code'),
        );
        if(count($data)>=4 and $id){
            $check = Menu::where('title',$data['title'])->where('id','<>',$id)->select();
            if($check){
                $res = ['statusCode'=>300,'message'=>'分类已存在'];
            }else{
                if($update = Menu::update($data)){
                    $res = ['statusCode'=>200,'message'=>'操作成功'];
                }else{
                    $res = ['statusCode'=>300,'message'=>'操作失败'];
                }
            }
        }else{
            $res = ['statusCode'=>300,'message'=>'数据录入错误'];
        }

        echo json_encode($res);
    }

    public function set(){
        $status = $this->request->param('status');
        $id = $this->request->param('id');
        if($id){
            Menu::where('id',$id)->update(['status'=>$status]);
            $res = ['statusCode'=>200,'message'=>'操作完成'];
        }else{
            $res = ['statusCode'=>300,'message'=>'非法操作'];
        }
        echo json_encode($res);
    }

    public function sort(){
        $sort = $this->request->param('sort');
        $id = $this->request->param('id');
        if($id){
            Menu::where('id',$id)->update(['sort'=>$sort]);
            $res = ['statusCode'=>200,'message'=>'操作完成'];
        }else{
            $res = ['statusCode'=>300,'message'=>'非法操作'];
        }
        echo json_encode($res);
    }

    public function delete(){
        $ids = explode(',',$this->request->param('ids'));
        $i = 0;
        if(count($ids)>0){
            foreach($ids as $key=>$value){
                $menu = Menu::get($value);
                if($menu->status==0){
                    Menu::destroy($value);
                    $i++;
                }
            }
            if($i>0){
                $res = ['statusCode'=>200,'message'=>'操作完成'];
            }else{
                $res = ['statusCode'=>300,'message'=>'操作失败,请检查您操作的项是否已禁用'];
            }
        }else{
            $res = ['statusCode'=>300,'message'=>'操作失败'];
        }
        echo json_encode($res);
    }

}